﻿package kliment.utils {
	import kliment.math.Calc;
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.0.3
	 */
	public class RandomList {
		private var _firstIndex:Number = 0;
		private var _lastIndex:Number = 0;
		private var _prevIndex:Number = 0;
		private var _currentIndex:Number;
		private var _list:Array;
		private var _prevList:Array;
		
		public function RandomList(firstIndex:Number, lastIndex:Number) {
			_firstIndex = firstIndex;
			_lastIndex = lastIndex;
			_prevList = new Array();
			_init();
		}
		
		private function _init():void {
			_list = new Array();
			for (var i:Number = _firstIndex; i <= _lastIndex; ++i) 
				_list.push(i);
		}
		
		public function get nextIndex():Number {
			if (_prevIndex < _prevList.length - 1) 
				return _prevList[++_prevIndex];
			if (!_list.length) 
				_init();
			var i:Number = Calc.randomRange(0, _list.length - 1);
			var result:Number = _list.splice(i, 1)[0];
			_currentIndex = result || _firstIndex;
			_prevList.push(_currentIndex);
			_prevIndex = _prevList.length - 1;
			return result;
		}
		
		public function get prevIndex():Number {
			_prevIndex = Math.max(0, --_prevIndex);
			var result:Number = _prevList[_prevIndex];
			_currentIndex = result || _firstIndex;
			return result;
		}
		
		public function get currentIndex():Number {
			return _currentIndex;
		}
		
		public function get remain():Number {
			return _list.length;
		}
		
	}

}